package com.hiultra.archive.controller;

import com.hiultra.archive.service.IArcShelvesService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.domain.entity.ArcShelves;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 密集架Controller
 *
 * @author ls
 * @date 2022-07-28
 */
@RestController
@RequestMapping("/deposit/shelves")
@Api(tags="密集架Controller")
public class ArcShelvesController extends BaseController {
    @Autowired
    private IArcShelvesService arcShelvesService;

    /**
     * 查询密集架列表
     */
    @ApiOperation(value = "查询密集架列表", notes = "查询密集架列表")
    @PreAuthorize("@ss.hasPermi('deposit:shelves:list')")
    @GetMapping("/list")
    public AjaxResult list(ArcShelves arcShelves) {
        List<ArcShelves> list = arcShelvesService.selectArcShelvesList(arcShelves);
        return AjaxResult.success(list);
    }

    /**
     * 导出密集架列表
     */
    @ApiOperation(value = "导出密集架列表", notes = "导出密集架列表")
    @PreAuthorize("@ss.hasPermi('deposit:shelves:export')")
    @Log(title = "密集架", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcShelves arcShelves) {
        List<ArcShelves> list = arcShelvesService.selectArcShelvesList(arcShelves);
        ExcelUtil<ArcShelves> util = new ExcelUtil<ArcShelves>(ArcShelves.class);
        util.exportExcel(response, list, "密集架数据");
    }

    /**
     * 获取密集架详细信息
     */
    @ApiOperation(value = "获取密集架详细信息", notes = "获取密集架详细信息")
    @PreAuthorize("@ss.hasPermi('deposit:shelves:query')")
    @GetMapping(value = "/{shelvesId}")
    public AjaxResult getInfo(@PathVariable("shelvesId") Long shelvesId) {
        return AjaxResult.success(arcShelvesService.selectArcShelvesByShelvesId(shelvesId));
    }

    /**
     * 新增密集架
     */
    @ApiOperation(value = "新增密集架", notes = "新增密集架")
    @PreAuthorize("@ss.hasPermi('deposit:shelves:add')")
    @Log(title = "密集架", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcShelves arcShelves) {
        return toAjax(arcShelvesService.insertArcShelves(arcShelves));
    }

    /**
     * 修改密集架
     */
    @ApiOperation(value = "修改密集架", notes = "修改密集架")
    @PreAuthorize("@ss.hasPermi('deposit:shelves:edit')")
    @Log(title = "密集架", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcShelves arcShelves) {
        return toAjax(arcShelvesService.updateArcShelves(arcShelves));
    }

    /**
     * 删除密集架
     */
    @ApiOperation(value = "删除密集架", notes = "删除密集架")
    @PreAuthorize("@ss.hasPermi('deposit:shelves:remove')")
    @Log(title = "密集架", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shelvesIds}")
    public AjaxResult remove(@PathVariable Long[] shelvesIds) {
        return toAjax(arcShelvesService.deleteArcShelvesByShelvesIds(shelvesIds));
    }

    /**
     * 设置密集架规格
     */
    @ApiOperation(value = "设置密集架规格", notes = "设置密集架规格")
    //@PreAuthorize("@ss.hasPermi('base:cabinet:add')")
    @Log(title = "设置密集架规格", businessType = BusinessType.INSERT)
    @PostMapping("/updateColumnAndSection")
    public AjaxResult updateColumnAndSection(@RequestBody ArcShelves arcShelves) {
        return toAjax(arcShelvesService.updateColumnAndSection(arcShelves));
    }

    /**
     *获取密集架下拉框数据
     */
    @ApiOperation(value = "获取密集架下拉框数据", notes = "获取密集架下拉框数据")
    @GetMapping("/treeselectShelves/{shelvesId}")
    public AjaxResult treeselectShelves(@PathVariable Long shelvesId)
    {
        List<ArcShelves> shelves = arcShelvesService.selectArcShelvesList(new ArcShelves(){{setZshelvesId(shelvesId);}});
        return AjaxResult.success(arcShelvesService.buildShelvesTreeSelect(shelves));
    }
    /**
     *获取密集架下拉框数据
     */
    @ApiOperation(value = "获取密集架下拉框数据", notes = "获取密集架下拉框数据")
    @GetMapping("/treeselectShelvesAll")
    public AjaxResult treeselectShelvesAll()
    {
        List<ArcShelves> shelves = arcShelvesService.selectArcShelvesList(null);
        return AjaxResult.success(arcShelvesService.buildShelvesTreeSelect(shelves));
    }


    /**
     * 密集架层架标签信息
     **/
    @ApiOperation(value = "密集架层架标签信息", notes = "密集架层架标签信息")
    @PostMapping("selectListAllLayer")
    public TableDataInfo selectListAllLayer(ArcShelves query ){
        startPage();
        List<ArcShelves> li = arcShelvesService.selectArcShelvesByLeve(query);
        List<ArcShelves> shelvesList = arcShelvesService.selectArcShelvesList(query);
        Map<String, ArcShelves> shelvesMap = shelvesList.stream().collect(Collectors.toMap(ArcShelves::getShelvesCode, arcShelves -> arcShelves));
        for (ArcShelves a : li) {
            List<String> str = new ArrayList<String>();
            //处理密集架名称
            String name = "";
            if (a.getShelvesCode().length() >= 9) {
                String allName = "";
                //密集架
                String shelvesName = shelvesMap.get(a.getShelvesCode().substring(0, 2)).getShelvesName();
                //根据code 查询密集架名称
                //橱
                String cabinetName = a.getShelvesCode().substring(3, 4) + "橱";
                //面
                String faceName = a.getShelvesCode().substring(4, 5) + "面";
                //列
                String columnName = a.getShelvesCode().substring(6, 7) + "列";
                //层
                String layerName = a.getShelvesCode().substring(8, 9) + "层";
                allName = shelvesName + cabinetName + faceName + columnName + layerName;
                a.setShelvesName(allName);
            }
        }
        return getDataTable(li);
    }
}
